package pl.edu.agh.io.gpstracker.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import pl.edu.agh.io.gpstracker.model.Node;
import pl.edu.agh.io.gpstracker.model.Tracksequence;

/**
 * 
 * @author wkedzior
 * 
 */
@Repository
public class TrackDaoImpl extends GenericDaoImpl<Tracksequence, Long> implements
		TrackSequenceDao {

	public TrackDaoImpl() {
		super(Tracksequence.class);
	}
	
	@Override
	public void saveOrUpdate(Tracksequence entity) {
		if (entity.getNode()!=null){
			getHibernateTemplate().save(entity.getNode());
		}
		getHibernateTemplate().merge(entity);
	}


	// ? nie ma maxResult w hibernate templte!!!! bleee
	@SuppressWarnings("unchecked")
	@Override
	public Tracksequence getLastPosition(Long trackId) {
		List<Tracksequence> trackSequences = getHibernateTemplate()
				.find(
						"from pl.edu.agh.io.gpstracker.model.Tracksequence where trackId = ? order by id desc",
						new Object[] { trackId });
		if (null != trackSequences && !trackSequences.isEmpty()) {
			return trackSequences.iterator().next();
		} else {
			return null;
		}
	}
	
/*	// ? nie ma maxResult w hibernate templte!!!! bleee
	@SuppressWarnings("unchecked")
	@Override
	public Tracksequence getLastPosition(Long trackId) {
		List<Node> trackSequences = getHibernateTemplate()
				.find(
						"from pl.edu.agh.io.gpstracker.model.Node where trackId order by id desc");
	
		if (null != trackSequences && !trackSequences.isEmpty()) {
			Tracksequence seq = new Tracksequence();
			seq.setNode(trackSequences.iterator().next());
			return seq;
		} else {
			return null;
		}
	}*/
}
